package sz.szsmk.citizencard.util;

import android.content.Context;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.TreeMap;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.request.IBaseRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.BaseRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.CommonRequest;
import sz.szsmk.citizencard.model.network.dzbean.res.PreAuthResult;
import sz.szsmk.citizencard.widget.GlobalVariables;

public class ObjectSignatureUtil {

    public static CommonRequest getCommonRequest() {
        Context context = DaggerApplication.getIns();
        PreAuthResult preAuthResult = GlobalVariables.getInstance(context).getPreAuthResult();
        CommonRequest commonRequest = new CommonRequest();
        if (preAuthResult == null) {
            return null;
        }
        commonRequest.setOpenId(preAuthResult.getOpenId());
        signatureRequest(commonRequest, preAuthResult.getToken());
        return commonRequest;
    }

    public static void signaturePreAuthRequest(BaseRequest obj) {
//        String key = RequestParamUtils.getKey();
//        signatureRequest(obj, "HV1KZEUBIBX934ZG");

        String channelId = ACache.get(DaggerApplication.getIns()).getAsString("channelId");
        //现在只有南京银行
        signatureRequest(obj, "CNNMPRI6Q8SGOKKR");

//        if (channelId == null) {
//            //南京银行
//            signatureRequest(obj, "6YO2TTKKGVOWOM0S");
//        } else if (channelId.equals("000001")) {
//            //南京银行
//            signatureRequest(obj, "6YO2TTKKGVOWOM0S");
//        } else if (channelId.equals("000003")) {
//            //建设银行
//            signatureRequest(obj, "CNNMPRI6Q8SGOKKR");
//        }

    }
    public static void signaturePreAuthRequestNew(BaseRequest obj, String channelId) {
//        String key = RequestParamUtils.getKey();
//        signatureRequest(obj, "HV1KZEUBIBX934ZG");

//        String channelId = ACache.get(DaggerApplication.getIns()).getAsString("channelId");
        if (channelId == null) {
            //南京银行
            signatureRequest(obj, "6YO2TTKKGVOWOM0S");
        } else if (channelId.equals("000001")) {
            //南京银行
            signatureRequest(obj, "6YO2TTKKGVOWOM0S");
        } else if (channelId.equals("000003")) {
            //建设银行
            signatureRequest(obj, "CNNMPRI6Q8SGOKKR");
        }

    }

    public static void signatureZhsz(BaseRequest re, String token) {
//        Map<String, String> stringMap = toMap(re);
        String token1 = ACache.get(DaggerApplication.getIns()).getAsString("token");
//        re.setChannelId();
        signatureRequest(re, token1);
    }

    public static void signatureRequest(CommonRequest obj) {
        Context context = DaggerApplication.getIns();
        PreAuthResult preAuthResult = GlobalVariables.getInstance(context).getPreAuthResult();
        if (preAuthResult == null) {
            return;
        }
        obj.setOpenId(preAuthResult.getOpenId());
        Map<String, String> stringMap = toMap(obj);
        String signature = SIgnUtil.generateSign(stringMap, preAuthResult.getToken());
        obj.setSignature(signature);
    }

    public static void signatureRequest(BaseRequest obj, String key) {
        Map<String, String> stringMap = toMap(obj);
        String signature = SIgnUtil.generateSign(stringMap, key);
        L.e(Constant.TAG, "signature------->" + signature);
        obj.setSignature(signature);
    }

    public static void signatureRequestZhsz(IBaseRequest obj) {
        Map<String, String> stringMap = toMap(obj);
        String refresh_token = ACache.get(DaggerApplication.getIns()).getAsString("refresh_token");

        String mkey = stringMap + refresh_token;
        String signature = SIgnUtil.generateSign(stringMap, mkey);
        obj.setSign(signature);
    }

    private static Map<String, String> toMap(Object obj) {
        Class<?> aClass = obj.getClass();
        Map<String, String> maps = new TreeMap<>();
        while (null != aClass && !aClass.getName().equals("java.lang.Object")) {
            Method[] declaredMethods = aClass.getDeclaredMethods();
            for (Method declaredMethod : declaredMethods) {
                if (declaredMethod.getName().startsWith("get")) {
                    String methodName = declaredMethod.getName();
                    String upField = methodName.substring(3, methodName.length());
                    String sField = upField.substring(0, 1).toLowerCase() + upField.substring(1, upField.length());
                    try {
                        Object retVal = declaredMethod.invoke(obj);
                        if (null != retVal) {
                            maps.put(sField, retVal.toString());
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }

                }
            }
            aClass = aClass.getSuperclass();
        }
        return maps;

    }

}